/*
 * Copyright (c) 2011.
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.graphoman.gui;

import org.graphoman.Action;
import org.graphoman.model.Model;
import org.graphoman.model.State;
import org.graphoman.model.Transition;
import org.graphoman.undo.CreateAction;
import org.graphoman.undo.DeleteAction;
import org.graphoman.undo.UndoAction;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: daniil 
 * Date: 11.10.11 
 * Time: 10:53 To change
 */
public class TransitionDialog extends JFrame {
	JComboBox fromComboBox;
	JComboBox toComboBox;
	JButton switchStatesButton;
	JTextField intensityField;
	JButton saveButton;
	JButton deleteButton;

    public static void showEditDialog(final Action action, final Model model, final Transition transition, final List<UndoAction> undoActions) {
		final TransitionDialog dialog = new TransitionDialog(action, model, transition, undoActions);
		dialog.setVisible(true);

	}

	public static void showCreateDialog(final Action action, final Model model, final Transition transition, final List<UndoAction> undoActions) {
		final TransitionDialog dialog = new TransitionDialog(action, model, transition, undoActions);
		dialog.getSaveButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				model.transitions.add(transition);
                undoActions.add(new CreateAction(transition));
			}
		});
		dialog.getDeleteButton().setVisible(false);
		dialog.setVisible(true);
	}

	public TransitionDialog(final Action action, final Model model, final Transition transition, final List<UndoAction> undoActions) {
		setLayout(new BorderLayout());
		JPanel contentPanel = new JPanel(new GridLayout(4, 2, 20, 20));
		contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

		JLabel fromLabel = new JLabel("from");
		contentPanel.add(fromLabel);
		fromComboBox = new JComboBox(model.states.toArray());
		fromComboBox.setSelectedItem(transition.from);
		contentPanel.add(fromComboBox);
		
		JLabel switchStatesLabel = new JLabel("switch");
		contentPanel.add(switchStatesLabel);
		switchStatesButton = new JButton(new ImageIcon("icons/oxygen/system_switch_user.png"));
		switchStatesButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				Object temp = fromComboBox.getSelectedItem();
				fromComboBox.setSelectedItem(
						toComboBox.getSelectedItem()
						);
				toComboBox.setSelectedItem(temp);
				
			}
		});
		switchStatesButton.setSize(32, 32);
		contentPanel.add(switchStatesButton);

		JLabel toLabel = new JLabel("to");
		contentPanel.add(toLabel);
		toComboBox = new JComboBox(model.states.toArray());
		toComboBox.setSelectedItem(transition.to);
		contentPanel.add(toComboBox);

		JLabel symbol = new JLabel("intensity");
		contentPanel.add(symbol);

		intensityField = new JTextField();
		contentPanel.add(intensityField);
		intensityField.setText(transition.intensity);

		add(contentPanel, BorderLayout.CENTER);

		JPanel buttonPanel = new JPanel();
		saveButton = new JButton("save");
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				transition.from = (State) getFromComboBox().getSelectedItem();
				transition.to = (State) getToComboBox().getSelectedItem();
				transition.intensity = getIntensityField().getText();
				if (action != null) {
					// repaint
					action.perform();

				}
				setVisible(false);

			}
		});

		JButton cancelButton = new JButton("cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				setVisible(false);

			}
		});

		deleteButton = new JButton("delete");
		deleteButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				model.transitions.remove(transition);
                undoActions.add(new DeleteAction(transition));
				setVisible(false);
				action.perform();

			}
		});
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);
		buttonPanel.add(deleteButton);
		add(buttonPanel, BorderLayout.SOUTH);

		setLocationRelativeTo(null);
		pack();
	}

	public JComboBox getFromComboBox() {
		return fromComboBox;
	}

	public JComboBox getToComboBox() {
		return toComboBox;
	}

	public JTextField getIntensityField() {
		return intensityField;
	}

	public JButton getSaveButton() {
		return saveButton;
	}

	public JButton getDeleteButton() {
		return deleteButton;
	}

}
